package biz.bbtec.sbm.email.action;

import java.sql.ResultSet;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import nu.xom.xinclude.MisplacedFallbackException;

import org.apache.struts2.interceptor.SessionAware;

import com.mysql.jdbc.EscapeTokenizer;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import biz.bbtec.sbm.email.bean.MailSenderInfo;
import biz.bbtec.sbm.email.factory.EmailAlarmFactory;
import biz.bbtec.sbm.email.service.EmailAlarmService;
import biz.bbtec.sbm.issuetracking.bean.TaskBean;
import biz.bbtec.sbm.issuetracking.bean.WorkBean;
import biz.bbtec.sbm.user.bean.UserBean;

@SuppressWarnings("serial")
public  class EmailAlarmAction extends ActionSupport implements
		SessionAware {
	private List<UserBean> userlist;
	private TaskBean task = new TaskBean();
     int i=0;
	public TaskBean getTask() {
		return task;
	}

	public void setTask(TaskBean 	task) {
		this.task = task;
	}

	public List<UserBean> getUserlist() {
		return userlist;
	}

	public void setUserlist(List<UserBean> userlist) {
		this.userlist = userlist;
	}
	private MailSenderInfo msi = new MailSenderInfo();
	public String NoticeSend() throws Exception {
		MailSenderInfo mailInfo = new MailSenderInfo();
		mailInfo.setMailServerHost("smtp.exmail.qq.com");
		mailInfo.setMailServerPort("25");
		mailInfo.setValidate(true);
		mailInfo.setUserName("sbm-service@bbtec.biz");
		mailInfo.setPassword("bb123456");// 您的邮箱密码
		mailInfo.setFromAddress("sbm-service@bbtec.biz");

		Map session = ActionContext.getContext().getSession();
		UserBean user = (UserBean) session.get("user");
		
		EmailAlarmService service = EmailAlarmFactory.getInstance().createEmailAlarmService();
		for (int i = 0; i < userlist.size(); i++) {
			UserBean userbean = service.User(userlist.get(i).getUserid());
			if(userbean.getEmail() == null){
				continue;
			}
			mailInfo.setToAddress(userbean.getEmail());
			mailInfo.setSubject("邀请通知");
			String context = "您好：\n" + user.getName() + "邀请您加入"
					+ task.getName() + "该项目，请您尽快登录sbm系统处理！";
			mailInfo.setContent(context);
			SimpleMailSender sms = new SimpleMailSender();
			sms.sendHtmlMail(mailInfo);// 发送html格式
		}

		return SUCCESS;
	}
	@SuppressWarnings("unchecked")
	public String TimingEmailSend() throws ParseException {
		boolean haswork=false;
		msi.setMailServerHost("smtp.qq.com");
		msi.setMailServerPort("25");
		msi.setValidate(true);
		msi.setUserName("sbm-service@bbtec.biz");
		msi.setPassword("bb123456");// 您的邮箱密码
		msi.setFromAddress("sbm-service@bbtec.biz");
		EmailAlarmService ea = EmailAlarmFactory.getInstance()
				.createEmailAlarmService();
		List<UserBean> userlist = new ArrayList<UserBean>();
		userlist = ea.userlist(); // get all user and  email if alertstate=on 
		if (userlist.size() != 0) {
			
			for (int i = 0; i < userlist.size(); i++) {
				List<TaskBean> tasklist = ea.tasklist(userlist.get(i)
						.getUserid());
				StringBuffer message = new StringBuffer("<font color='red'>"
						+ userlist.get(i).getName()
						+ "</font>,您好！<br> 以下是您快到期或已逾期的工作包,请尽快前往http://sbm.bbtec.biz 更新进度：<br/>");
				if (tasklist.size() != 0) {
					for (int j = 0; j < tasklist.size(); j++) {
						List<WorkBean> worklist = ea.worklist(userlist.get(i)
								.getUserid(), tasklist.get(j).getTaskid());
						if (worklist.size() != 0) {
							List<WorkBean> result = ea.check(worklist);
							if (result.size() != 0) {
								haswork=true;
								msi.setToAddress(userlist.get(i).getEmail());
								msi.setSubject("您有工作任务已到期");
								StringBuffer title = new StringBuffer("属于<B>"
										+ tasklist.get(j).getName()
										+ "</B>该项目的工作包是:<br/>");
								message.append(title);
				
								for (int k = 0; k < result.size(); k++) {
									StringBuffer temp = new StringBuffer(result
											.get(k).getTitle()
											+ "(截止时间:"
											+ result.get(k).getDeadline()
													.trim() + ")" + "<br/>");
									message.append(temp);
								}
							}
						}				
					}
				}			
				msi.setContent(message.toString());
				if(haswork){
					ea.mailsend(msi);
				}
				haswork=false;
			}
		}
		return SUCCESS;

	}
	
	public String AlarmEmailSend(){
		msi.setMailServerHost("smtp.qq.com");
		msi.setMailServerPort("25");
		msi.setValidate(true);
		msi.setUserName("sbm-service@bbtec.biz");
		msi.setPassword("bb123456");// 您的邮箱密码
		msi.setFromAddress("sbm-service@bbtec.biz");
		EmailAlarmService ea = EmailAlarmFactory.getInstance()
				.createEmailAlarmService();
		
		// set mail Content
		
		ea.mailsend(msi);		
			return SUCCESS;
	}


	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		
	}
}